package com.toe.admin.controller;

import com.toe.common.restful.JsonResult;
import com.toe.common.utils.FileUtils;
import com.toe.pojo.dto.recommend.RecommendAddClassDTO;
import com.toe.pojo.entity.recommend.RecommendClasses;
import com.toe.recommend.service.RecommendClassesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/recommend")
@Api(tags = "推荐普通课程")
public class recommendController {
    @DubboReference
    private RecommendClassesService dubboRecommendService;

    @ApiOperation(value = "普通课程上架")
    @PostMapping("/free-class/add")
    public JsonResult<Void> insert(RecommendAddClassDTO classDTO, MultipartFile file) {
        dubboRecommendService.insert(classDTO, FileUtils.multipartFileToFile(file));
        return JsonResult.ok();
    }

    @ApiOperation(value = "审核通过")
    @GetMapping("/class/check")
    public JsonResult<Void> checkFreeClass(Long id) {
        RecommendClasses recommendClasses = new RecommendClasses();
        recommendClasses.setId(id);
        recommendClasses.setIsChecked(1);
        dubboRecommendService.updateById(recommendClasses);

        return JsonResult.ok();
    }

    @ApiOperation(value = "普通课程下架")
    @PostMapping("/free-class/delete")
    public JsonResult<Void> deleteFreeClass(Long id) {
        dubboRecommendService.removeById(id);
        return JsonResult.ok();
    }

}
